package cevent.source.cloudcenter.server.service;

import cevent.source.cloudcenter.server.domain.${Domain};
import cevent.source.cloudcenter.server.domain.${Domain}Example;
import cevent.source.cloudcenter.server.dto.${Domain}Dto;
import cevent.source.cloudcenter.server.dto.PageDto;
import cevent.source.cloudcenter.server.mapper.${Domain}Mapper;
import cevent.source.cloudcenter.server.enums.*;
import cevent.source.cloudcenter.server.util.DuplicateUtil;
import cevent.source.cloudcenter.server.util.UUID8Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author cevent
 * @description ${tableNameCN}服务层
 */
@Service
public class ${Domain}Service {
    private static final Logger LOG=LoggerFactory.getLogger(${Domain}Service.class);
    @Resource
    private ${Domain}Mapper ${domain}Mapper;

    //1.无分页列表查询：dto获取domain参数
    public List<${Domain}Dto> ${domain}DtoList(){
        ${Domain}Example ${domain}Example = new ${Domain}Example();
        ${domain}Example.setOrderByClause("uni_id asc");
        List<${Domain}> ${domain}List=${domain}Mapper.selectByExample(${domain}Example);
        List<${Domain}Dto> ${domain}DtoList = new ArrayList<${Domain}Dto>();
        for(int i=0;i<${domain}List.size();i++){
            ${Domain} ${domain}=${domain}List.get(i);
            ${Domain}Dto ${domain}Dto=new ${Domain}Dto();
            //复制拷贝(source,target)
            BeanUtils.copyProperties(${domain},${domain}Dto);
            ${domain}DtoList.add(${domain}Dto);
        }
        return ${domain}DtoList;
    }

    /*2.分页查询：pageDto保存数据
        list数据集合：listPageData
        当前页码：currentPage
        每页行数：size
        总计函数：totalSize
        前端可返回pageDto，无需返回值
     */
    public void get${Domain}PageDtoList(PageDto pageDto){
        //1.分页设置（前端传入当前页和每页行数params），起始页，每页行数size
        PageHelper.startPage(pageDto.getCurrentPage(),pageDto.getSize());

        ${Domain}Example ${domain}Example=new ${Domain}Example();

        <#list fieldUtilList as field>
            <#if field.nameSmallHump=="sort">
                ${domain}Example.setOrderByClause("sort asc");
            </#if>
        </#list>

        List<${Domain}> ${domain}List=${domain}Mapper.selectByExample(${domain}Example);
        //2.将mapper查询结果复制到pageInfo
        PageInfo<${Domain}> pageInfo=new PageInfo<>(${domain}List);
        //3.pageInfo计算处理处理=>后分页的总行数set到pageDto做记录
        pageDto.setTotalSize(pageInfo.getTotal());

        List<${Domain}Dto> ${domain}DtoList=new ArrayList<>();
        ${domain}List.stream().forEach(${domain}->{
            ${Domain}Dto ${domain}Dto=new ${Domain}Dto();
            BeanUtils.copyProperties(${domain},${domain}Dto);
            ${domain}DtoList.add(${domain}Dto);
        });

        pageDto.setListPageData(${domain}DtoList);

    }

    //3.新增、修改${domain}，将传入的id转为${domain}对象本身
    public void save(${Domain}Dto ${domain}Dto){
        ${Domain} ${domain}= DuplicateUtil.copy(${domain}Dto,${Domain}.class);
        if(StringUtils.isEmpty(${domain}Dto.getUniId())){
            this.insert(${domain});
        }else{
            this.update(${domain});
        }

    }

    //4.删除模块
    public void delete(String uniId){
        ${Domain}Example ${domain}Example=new ${Domain}Example();
        ${domain}Mapper.deleteByPrimaryKey(uniId);
    }

    //5.向外暴露dto，不暴露实体类：插入数据
    private void insert(${Domain} ${domain}){
        ${domain}.setUniId(UUID8Util.getShortUUID());
        <#list fieldUtilList as field>
            <#if field.nameSmallHump=="moduleId">
        if(${domain}.get${field.nameBigHump}()==null){
            ${domain}.set${field.nameBigHump}("0");
        }
            </#if>
            <#if field.nameSmallHump=="parentId">
        if(${domain}.get${field.nameBigHump}()==null){
            ${domain}.set${field.nameBigHump}("0");
        }
            </#if>
            <#if field.nameSmallHump=="typeId">
        if(${domain}.get${field.nameBigHump}()==null){
            ${domain}.set${field.nameBigHump}(ModuleTypeEnum.BASE_MODULE.getCode());
        }
            </#if>
            <#if field.nameSmallHump=="videoUserId">
        if(${domain}.get${field.nameBigHump}()==null){
            ${domain}.set${field.nameBigHump}("0");
        }
            </#if>
            <#if field.nameSmallHump=="videoType">
        if(${domain}.get${field.nameBigHump}()==null){
            ${domain}.set${field.nameBigHump}(VideoTypeEnum.PUBLIC.getCode());
        }
            </#if>
            <#if field.nameSmallHump=="videoCharge">
        if(${domain}.get${field.nameBigHump}()==null){
            ${domain}.set${field.nameBigHump}(VideoChargeEnum.FREE.getCode());
        }
            </#if>
            <#if field.nameSmallHump=="level">
        if(${domain}.get${field.nameBigHump}()==null){
            ${domain}.set${field.nameBigHump}(LevelEnum.ONE.getCode());
        }
            </#if>
        </#list>

        try {
        Date now =new Date();
        String date=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(now);
        long time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date).getTime();
        int timeInt=(int)(time/1000);
        ${domain}.setCreateTime(now);
        ${domain}.setUpdateTime(now);
        ${domain}.setiCreateTime(timeInt);
        ${domain}.setiUpdateTime(timeInt);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        ${domain}Mapper.insert(${domain});
    }

    //6.更新模块
    private void update(${Domain} ${domain}){
        try {
            Date now =new Date();
            String date=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(now);
            long time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date).getTime();
            int timeInt=(int)(time/1000);

            <#list fieldUtilList as field>
            <#if field.nameSmallHump=="updateTime">
            ${domain}.set${field.nameBigHump}(now);
            </#if>
            <#if field.nameSmallHump=="iUpdateTime">
                ${domain}.set${field.nameSmallHump}(timeInt);
            </#if>
        </#list>
        } catch (ParseException e) {
            e.printStackTrace();
        }

        ${domain}Mapper.updateByPrimaryKey(${domain});
    }

}
